package com.fenxiangle.yueding.feature.mine.view;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.common.widget.dialog.ActionSheetDialog;
import com.fenxiangle.yueding.common.widget.dialog.OnOperItemClickL;
import com.fenxiangle.yueding.entity.bo.DeleteBo;
import com.fenxiangle.yueding.entity.bo.EventEntity;
import com.fenxiangle.yueding.entity.bo.UserCenterBo;
import com.fenxiangle.yueding.entity.bo.UserVideoBo;
import com.fenxiangle.yueding.feature.mine.view.adapter.VedioAdapter;
import com.fenxiangle.yueding.framework.api.UserApi;
import com.luck.picture.lib.rxbus2.RxBus;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.widget.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * A simple {@link Fragment} subclass.
 */
public class VedioOtherFragment extends BaseFragment implements BaseQuickAdapter.OnItemChildClickListener, OnOperItemClickL {


    @BindView(R.id.rv_vedio)
    RecyclerView rvVedio;
    private ActionSheetDialog dialog;
    private int mSelectPosition;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_vedio;
    }

    private VedioAdapter mVedioAdapter;
    private UserCenterBo userCenterBo;
    private boolean isAdd;
    private boolean isMine;

    @Override
    protected void initInjector() {

    }

    public static VedioOtherFragment getInstance(UserCenterBo userCenterBo, boolean isAdd, boolean isMine) {
        VedioOtherFragment sf = new VedioOtherFragment();
        sf.userCenterBo = userCenterBo;
        sf.isAdd = isAdd;
        sf.isMine = isMine;
        return sf;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    protected void initView() {
        final String[] stringItems = {"删除"};
        dialog = new ActionSheetDialog(getActivity(), stringItems, null);
        dialog.isTitleShow(false);
        dialog.setOnOperItemClickL(this);


        rvVedio.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        rvVedio.addItemDecoration(new SpacesItemDecoration(2, 16, false));
        mVedioAdapter = new VedioAdapter(isAdd);
        mVedioAdapter.setUserCenterBo(userCenterBo);
        mVedioAdapter.bindToRecyclerView(rvVedio);
        List<UserVideoBo> userVideoBos = userCenterBo != null && userCenterBo.getUserVideo() != null ? userCenterBo.getUserVideo() : null;
        mVedioAdapter.setNewData(userVideoBos);
        mVedioAdapter.setOnItemChildClickListener(this);
        mVedioAdapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
            @Override
            public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
                if (isMine) {
                    if (dialog != null) dialog.show();
                    mSelectPosition = position;
                }
                return false;
            }
        });
    }


    @Override
    protected void initData() {
        RxBus.getDefault().register(this);
    }

    @Override
    protected void initEvent() {

    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.video_img:
                if (!TextUtils.isEmpty(userCenterBo.getUserVideo().get(position).getVideoUrl())) {
                    Intent intent = new Intent(getActivity(), VideoPlayerActivity.class);
                    intent.putExtra("userCenterBo", userCenterBo);
                    intent.putExtra("videoCoverImg", userCenterBo.getUserVideo().get(position).getVideoCover());
                    intent.putExtra("videoPath", userCenterBo.getUserVideo().get(position).getVideoUrl());
                    intent.putExtra("dec", userCenterBo.getUserVideo().get(position).getVideoDescribe());
                    startActivity(intent);
                }
                break;

            case R.id.btn_show_upload:
                Intent intent = new Intent(getActivity(), UploadVedioActivity.class);
                intent.putExtra("userCenterBo", userCenterBo);
                intent.putExtra("position", position);
                startActivity(intent);
                break;
        }

    }

    @Override
    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
        List<String> strings = new ArrayList<>();
        DeleteBo deleteBo = new DeleteBo();
        strings.add(userCenterBo.getUserVideo().get(mSelectPosition).getId());
        deleteBo.setVideoIds(strings);
        deleteVedio(deleteBo);
    }

    private void deleteVedio(DeleteBo urls) {
        AM.api().createApiService(UserApi.class)
                .deleteVedio(urls)
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        dismissLoading();
                        showMsg("删除成功!");
                        if (dialog != null) dialog.dismiss();
                        RxBus.getDefault().post(new EventEntity(999, "a"));
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();
                        showMsg(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }
}
